/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;
import pojo.Cuahang;
import util.HibernateUtil;

/**
 *
 * @author Cong
 */
public class daoCuaHang {
    
    public static Cuahang GetCH(Cuahang ch) {
        Cuahang kq = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            kq = (Cuahang)sS.get(Cuahang.class, ch.getId());
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static List<Cuahang> LaydsCuaHang() {
        List<Cuahang> ds = null;
        String sql = "from Cuahang order by id asc";
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Query qr = sS.createQuery(sql);
            ds = qr.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static List<Cuahang> FindCH(Cuahang ch) {
        List<Cuahang> kq = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria cR = sS.createCriteria(Cuahang.class);
            cR.add(Restrictions.ilike("tencuahang","%" + ch.getTencuahang() + "%"));
            cR.add(Restrictions.ilike("diachi","%" + ch.getDiachi() + "%"));
            kq = cR.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean LuuCH(Cuahang ch) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Cuahang temp = GetCH(ch);
        if (temp == null) {
            return false;
        }
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.update(ch);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean XoaCH(Cuahang ch) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Cuahang temp = GetCH(ch);
        if (temp == null) {
            return false;
        }
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.delete(temp);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean ThemCuaHang(Cuahang ch) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.save(ch);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            kq = false;
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
}
